﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NDde.Server;
using System.IO;

namespace lCash
{
    public delegate void myDelegate(string tName);
    public class MyDDEServer : DdeServer
    {
        //public delegate void OnPokeFinishedHandler(object sender,                
        StreamWriter sw;
        private string[,] strmtx;
        double[,] mtx;
        public event myDelegate MyEbent;
        public MyDDEServer(string service)
            : base(service)
        {
            sw = new StreamWriter("d:\\1.txt");

        }

        public string[,] result
        {
            get
            {
                return strmtx;
            }
            private set
            {
            }
        }
        public double[,] resultNumbers
        {
            get
            {
                return mtx;
            }
            private set
            {
            }
        }   
        protected override PokeResult OnPoke(DdeConversation conversation,
                string item, byte[] data, int format)
        {            
            XLTable tbl = new XLTable(data);
            mtx = new double[tbl.Rows, tbl.Columns];
            strmtx = new string[tbl.Rows, tbl.Columns];
            XLTable.BytesToMatrix(data, ref mtx, ref strmtx);
            float bd = 0, mrgn = 0;
            int ticketcol = -1, pricecol = -1, ordercol = -1, statuscol = -1;          
            float deposit,profit;
            bool updated = false;
            /*
             switch (conversation.Topic) 
                {
                    case "portfolio":
                        for (int i = 0; i < strmtx.GetLength(1); i++)
                        {
                            if (strmtx[0, i] == "Вход.активы")
                                bd = (float)mtx[1, i];
                            if (strmtx[0, i] == "Плечо")
                                mrgn = (float)mtx[1, i];
                            if (strmtx[0, i] == "Прибыль/убытки")
                                profit = (float)mtx[1, i];
                        }
                        deposit = bd * (mrgn + 1);
                        break;
                    case "orders": 
                        MyEbent(conversation.Topic);               
                     
                        for (int i = 0; i < strmtx.GetLength(1); i++)
                        {
                            if (strmtx[0, i] == "Номер")
                                ordercol = i;
                            if (strmtx[0, i] == "Состояние")
                                statuscol = i;
                            //sw.WriteLine()
                        }                        
                        break;                    
                }
            */
                updated = true;                        
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
                sb.Append(data[i]);
            sw.WriteLine("OnPoke:".PadRight(16)
                    + " Service=" + conversation.Service
                    + " Topic=" + conversation.Topic
                    + "\n\tDATA(XLTable must be parsed to business objects):\t".PadRight(16) + sb.Append('\n'));
            MyEbent(conversation.Topic);               
            return PokeResult.Processed;
        }

        protected override void OnDisconnect(DdeConversation conversation)
        {
            sw.WriteLine("OnDisconnect:".PadRight(16)
                        + " Service=" + conversation.Service
                        + " Topic=" + conversation.Topic);
            sw.Close();
        }

        protected override void OnAfterConnect(DdeConversation conversation)
        {
            sw.WriteLine("OnAfterConnect:".PadRight(16)
                        + " Service=" + conversation.Service
                        + " Topic=" + conversation.Topic);
        }
        protected override bool OnBeforeConnect(string topic)
        {
            sw.WriteLine("OnBeforeConnect:".PadRight(16)
                        + " Service=" + base.Service
                        + " Topic=" + topic);

            return true;
        }
    }

}